Skip to content

[AMD] CanonicalizePointers: Handle different base pointers and offsets#9541

Open
kelesvol wants to merge 1 commit intotriton-lang:mainfrom
kelesvol:canon-ptr
Open

[AMD] CanonicalizePointers: Handle different base pointers and offsets#9541
kelesvol wants to merge 1 commit intotriton-lang:mainfrom
kelesvol:canon-ptr

Conversation

@kelesvol
Copy link
Contributor

scf.if and arith.select ops can have different base pointers accross branches. In that case, FatPtrAttrs can't keep smallTensorBase or pick one of the branches. Instead, replace smallTensorBase (Value) with isSmallTensor (bool) in FatPtrAttrs and add a conservative merge function for combining attrs. Also, handle different scalar offsets properly when handling arith.select ops.

@kelesvol
Copy link
Contributor Author

cc @AlexAUT

Copy link
Contributor

@AlexAUT AlexAUT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this, looks reasonable to me. cc @yangshuxin since he also touched this pass IIRC.

return !(lhs == rhs);
}

static FatPtrAttrs merge(const FatPtrAttrs &lhs, const FatPtrAttrs &rhs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would intersect be a better name? merge sounds a bit like we take the union of all attributes.

lhs.attributes == rhs.attributes &&
lhs.smallTensorBase == rhs.smallTensorBase;
lhs.isSmallTensor == rhs.isSmallTensor &&
lhs.attributes == rhs.attributes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ptr1 and ptr2 is from small tensor a and b respectively, they share the same attribute, then == return true?.

I think this is fundamentally wrong although replacing smallTensorBase with a boolean does not expose bug.

Maybe you can add another field to indicate the case where the base is dynamic, it is a combination of multiple real tensors. say, introduce dynamic-base for that purpose.

When merging from different base, clobber the smallTensorBase, and set the dynamic-base.

Or conservertively, just clobber of tensorBase to indicate it too complicated to handle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ptr1 and ptr2 is from small tensor a and b respectively, they share the same attribute, then == return true?

No, we compare attributes here, not the actual pointers.

Maybe you can add another field to indicate the case where the base is dynamic, it is a combination of multiple real tensors. say, introduce dynamic-base for that purpose.

Where will we use dynamic base in this case? Currently, we keep smallTensorBase, but don't use it other than checking if it's a small tensor. With isSmallTensor added, we'll no longer need that either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we compare attributes here, not the actual pointers.

Then, why it's valid? Two pointers are equal if the share the same attribute even if they don't have the same base?

While I cannot give your examples as to why this will have incur problem at this moment (need to check the code), does not your change sound very dangerous? You claim two pointers are equals without comparing their base-pointer. It might works for now because, by current implementation, when two pointers involved into comparision, they may happen to have to same bases. It has huge potential problems.

We cannot weaken the condition at the huge risk of correctness just to make dealing dynamic-base slightly easier.

for (const auto &attr : lhs.attributes) {
auto it = rhs.attributes.find(attr.first);
if (it != rhs.attributes.end() && it->second == attr.second)
result.attributes[attr.first] = attr.second;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can replace the data-type to small vectormap?

// If the fat-pointer points to somewhere in a small-tensor, keep track the
// base of the tensor.
Value smallTensorBase;
bool isSmallTensor = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, we cannot say ptr-to-a == ptr-to-b.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, this is a part of the attributes, it doesn't mean pointers are the same.

`scf.if` and `arith.select` ops can have different base pointers accross
branches. In that case, `FatPtrAttrs` can't keep `smallTensorBase` or pick
one of the branches. Instead, replace `smallTensorBase` (Value) with
`isSmallTensor` (bool) in `FatPtrAttrs` and add a conservative `merge`
function for combining attrs. Also, handle different scalar offsets properly
when handling `arith.select` ops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants